package com.haoxuer.discover.user.shiro.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 扩展认证默认过滤
 *
 * @author ty
 */
public class FormAuthenticationCaptchaFilter extends FormAuthenticationFilter {

  private Logger logger = LoggerFactory.getLogger("ada");

  @Override
  protected boolean onLoginSuccess(AuthenticationToken token,
                                   Subject subject, ServletRequest request, ServletResponse response)
      throws Exception {
    // TODO Auto-generated method stub

    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpServletResponse = (HttpServletResponse) response;

    String url = this.getSuccessUrl();
    logger.info(url);
    return super.onLoginSuccess(token, subject, request, response);
  }

  public static final String DEFAULT_CAPTCHA_PARAM = "captcha";
  private String captchaParam = DEFAULT_CAPTCHA_PARAM;

  public String getCaptchaParam() {
    return captchaParam;
  }

  protected String getCaptcha(ServletRequest request) {
    return WebUtils.getCleanParam(request, getCaptchaParam());
  }

  @Override
  protected AuthenticationToken createToken(ServletRequest request,
                                            ServletResponse response) {
    String username = getUsername(request);
    String password = getPassword(request);
    if (password == null) {
      password = "";
    }
    String captcha = getCaptcha(request);
    boolean rememberMe = isRememberMe(request);
    String host = getHost(request);
    return new UsernamePasswordCaptchaToken(username,
        password.toCharArray(), rememberMe, host, captcha);
  }

}